package Data;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class StringDealer {

    public static String DataToString(BasicData data)
    {
        String m=String.format("{%s}{%d}{%s}{%s}{%.2f}",data.getCategory(),data.getType(),data.getBrand(),data.getName(),data.getPrice());
        if(data.getDate()!=null)
            m+=String.format("(d:%s)",data.DateStr().split(" ")[0]);
        if(data.getTag()!=null&&data.getTag().length()>0)
            m+=String.format("(t:%s)",data.getTag());
        return m;
    }

    public static Date strToDateLong(String strDate) {
        if(strDate==null)
            return null;

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }
    public static String[] PickUp(String text,char before,char after)
    {
        ArrayList<String> arr=new ArrayList<>();
        String t=null;
        for(int i=0;i<text.length();i++)
        {
           char c= text.charAt(i);
           if(c==before&&t==null)
           {
               t="";
               continue;
           }
            if(c==after&&t!=null)
            {
                arr.add(t);
                t=null;
                continue;
            }
           if(t!=null)
               t+=c;
        }
        String[] res=new String[arr.size()];
        arr.toArray(res);
        return res;
    }
}
